package cn.edu.ctbu.ty01.api;

import cn.edu.ctbu.ty01.constant.REnum;
import cn.edu.ctbu.ty01.entity.Student;
import cn.edu.ctbu.ty01.exception.RException;
import cn.edu.ctbu.ty01.service.StudentService;
import cn.edu.ctbu.ty01.util.Rutil;
import cn.edu.ctbu.ty01.vo.QueryObj;
import cn.edu.ctbu.ty01.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/student")
public class StudentApiController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/list")
    public R<List<Student>> findAll(){

        List<Student> students = studentService.getAll();
        return Rutil.success(students);
    }

    @GetMapping("/{id}")
    public R<Student> findById(@PathVariable int id){

        Student student = studentService.findById(id);
        return Rutil.success(student);
    }

    @PostMapping("/add")
    public R<Student> add(Student student){

        return Rutil.success(studentService.add(student));

    }
    @PostMapping("/update")
    public R<Student> update(Student student){

        return Rutil.success(studentService.update(student));

    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {

        studentService.delete(id);
        return Rutil.success();
    }

    @GetMapping("/validateUser")
    public R validateSnoAndPassword(String sno,String password) throws Exception{

        return Rutil.success(studentService.validateUsernameAndPassword(sno,password));
    }

    @PostMapping("/getbypage")
    public R<Page<Student>> getByPage(@RequestBody QueryObj<Student> qObj){
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        Integer pageIndex=0;
        Integer pageSize=10;

        if(qObj==null){
            Pageable pageable = PageRequest.of(pageIndex,pageSize,sort);
            Page<Student> students = studentService.getAll(pageable);
            return Rutil.success(students.getContent(),students.getTotalElements());
        }else{
            pageIndex= qObj.getPage()-1;
            pageSize= qObj.getLimit();

            if(qObj.getData() instanceof Student){
                Student student = (Student) qObj.getData();
                Pageable pageable = PageRequest.of(pageIndex,pageSize,sort);

                ExampleMatcher matcher = ExampleMatcher.matching()
                        .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains())
                        .withIgnoreNullValues();

                Example<Student> example = Example.of(student, matcher);
                Page<Student> studentPage = studentService.getAll(example, pageable);
                return Rutil.success(studentPage.getContent(),studentPage.getTotalElements());
            }else {
                throw new RException(REnum.QUERY_ERR);
            }
        }
    }


}
